/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software Foundation,
 *    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package JET.Frames.materialsframe;

import JET.physics.MaterialPair;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author 057Ry
 */
public class MaterialsCellRenderer extends JPanel implements TableCellRenderer
{

    private MaterialPair matP;
    private boolean selected = false;
    private Color color;

    public void paintComponent(Graphics g)
    {
        //super.paintComponent(g);

        g.setColor(this.color);
        g.fillRect(0, 0, getWidth(), getHeight());

        if (matP != null)
        {
            // Bounce
            g.setColor(Color.BLUE);
            g.drawString("B: "+matP.elasticity,10,20);
            // Friction
            g.setColor(new Color(0.f, 0.75f, 0.f));
            g.drawString("F: "+matP.friction, 10,30);
            // Collide
            g.setColor(Color.BLACK);
            g.drawString("C: "+matP.collide, 10, 40);
        }
        else
        {
            g.setColor(Color.RED);
            g.drawString("null",10,10);
        }
    }


    public void setBackground(Color color)
    {
        this.color = color;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
                                                    boolean isSelected, boolean hasFocus,
                                                    int row, int column)
    {
        System.out.println("row "+row+" column "+column);
        if (table != null)
        {
            //System.out.println("Cell isSelected= "+isSelected+" (r="+row+" c="+column+")");
            // on diag
            if (row == column)
            {
                this.color=Color.LIGHT_GRAY;
//                System.out.println("row==column");
            }
            else if (row>column)
            {
                this.color=Color.WHITE;
                //System.out.println("row>column");
            }
            else if (row<column)
            {
                this.color=new Color(183, 255, 102);
                //System.out.println("row<column");
            }

            if (isSelected)
            {
                this.color= new Color(.5f, .5f, 1.f);
            }
            
            //System.out.println("Color is set to "+this.color);

            Font font = table.getFont();
            setFont( new Font(font.getName(), font.getStyle(), 9));

            setEnabled(table.isEnabled());
        }
        else
        {
            System.out.println("MaterialsCellRenderer - Table null");
        }

        //Sets value
        if (value instanceof MaterialPair)
            matP = (MaterialPair)value;
        else
            matP = null;

        return this;
    }

}
